﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class StorageConfirm : Form
    {
        public StorageConfirm()
        {
            InitializeComponent();
        }

        DataTable DtOut;

        public void BindCMB()
        {
            DtOut = Bll_ProductIn.GetInStockProduct();
            cmb_BillNo.Text = "";
            cmb_BillNo.Items.Clear();
            if (DtOut.Rows.Count > 0)
            {
                foreach (DataRow dr in DtOut.Rows)
                {
                    cmb_BillNo.Items.Add(dr["BillNo"].ToString());
                }
            }
        }


        public void InputBillNo()
        {
            DataRow[] drOut = DtOut.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drOut.Length > 0)
            {
                DataTable info = Bll_ProductIn.GetInStockInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {

                        var dt = Bll_ProductIn.GetStorageConfirmInfo(cmb_BillNo.Text);
                        lab_msg.Text = GetMessage(dt);
                    }
                    else
                    {
                        MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        cmb_BillNo.Text = "";
                        cmb_BillNo.Focus();
                    }
                }
            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "单号不正确,请输入正确的单据!";
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();
            }
        }

        private void btn_confirm_Click(object sender, EventArgs e)
        {
            var billNo = cmb_BillNo.Text;
            if (string.IsNullOrEmpty(billNo))
            {
                MessageBox.Show("请先选择单号！");
                cmb_BillNo.Focus();
                return;
            }

            var relatedBill = string.Empty;

            var result = Bll_ProductIn.InStockStorageConfirm(billNo, out relatedBill);
            if (result)
            {
                if (relatedBill.StartsWith("RET"))
                {
                    Bll_ProductIn.InStockRETStorage(billNo);
                }

                lab_msg.Text = cmb_BillNo.Text + "入库确认成功!";
                BindCMB();

            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "入库确认失败!";

            }
        }

        private void StorageConfirm_Load(object sender, EventArgs e)
        {
            BindCMB();
        }

        public string GetMessage(DataTable dt)
        {
            var result = string.Empty;

            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    sb.AppendLine("物料号:" + dr["PartNumber"] + ";计划数量:" + dr["PlanQty"] + ";实际数量:" + dr["RealQty"]);
                }
                result = sb.ToString();

            }
            return result;
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }


    }
}